﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace MarkdownFilesCreator
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void miCreatFiles_Click(object sender, RoutedEventArgs e)
        {
            string destDirectory;

            //选择新工作目录
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog()
            {
                Description = "　　请选择一个文件作为导出目标。\r\n　　导出时会简单地将除Markdown文件以外的所有文件复制到目标文件夹中。",
                ShowNewFolderButton = true,
            };
            System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;

            System.Windows.Forms.IWin32Window win = new WinFormWindow(source.Handle);
            System.Windows.Forms.DialogResult result = fbd.ShowDialog(win);
            if (result.Equals(System.Windows.Forms.DialogResult.OK))
            {
                destDirectory = fbd.SelectedPath;
            }
            else return;

            if (destDirectory.EndsWith("\\") == false) destDirectory += "\\";

            string[] lines = tbx.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            DirectoryInfo curDirectoryInfo = null;
            FileInfo curFileInfo = null;
            StringBuilder sb = null;
            int sectionNumber = 0;
            int fileNumber = 1;

            foreach (var line in lines)
            {
                if (line.Length <= 1) continue;
                if (line.Contains("测试内容") || line.Contains("测试要求")) continue;

                if (IsNumber(line[0]) && line[1] == '．')
                {
                    //以数字开头的是专题名，应生成文件夹
                    curDirectoryInfo = Directory.CreateDirectory(destDirectory + line);
                    sectionNumber++;
                    fileNumber = 1;
                    continue;
                }

                if (line.Contains('\t'))
                {
                    if (curFileInfo != null && sb.Length > 0)
                    {
                        File.WriteAllText(curFileInfo.FullName, sb.ToString());
                    }
                    sb = null;//换新文件了

                    if (curDirectoryInfo == null) continue;

                    //带\t的，前半部分是节名，应作为一个新文件的名称；后半部分是节中的第一个标题，应作为节文件中的一个一级标题
                    string[] spans = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    if (spans.Length != 2) continue;

                    curFileInfo = new FileInfo(curDirectoryInfo.FullName.EndsWith("\\") ? curDirectoryInfo.FullName : (curDirectoryInfo.FullName + "\\") + fileNumber + "．" + spans[0] + ".md");

                    sb = new StringBuilder();
                    sb.Append("＜＜＜信息＞\n" +
                                "　类型＞＞教程＜＜类型\n" +
                                "　标题＞＞" + curDirectoryInfo.Name + "＜＜标题\n" +
                                "　日期＞＞" + DateTime.Now.ToString() + "＜＜日期\n" +
                                "　辑录＞＞杨震宇＜＜辑录\n" +
                                "　电邮＞＞LunarSF@163.com＜＜电邮\n" +
                                "　备注＞＞专题" + sectionNumber + "，第" + fileNumber + "节，" + spans[0] + "＜＜备注\n" +
                                "＜信息＞＞＞\n" +
                                "\n\n\n");
                    sb.Append("#" + spans[1] + "\n\n\n\n");

                    fileNumber++;
                    continue;
                }

                //其它文本行，都是某个节文件中的某个标题
                if (sb != null)
                {
                    sb.Append("#" + line + "\n\n\n\n");
                }
            }

            if (curFileInfo != null && sb.Length > 0)
            {
                File.WriteAllText(curFileInfo.FullName, sb.ToString());
            }

            MessageBox.Show("生成完毕！");
            System.Diagnostics.Process.Start("explorer.exe", destDirectory);
        }

        private bool IsNumber(char c)
        {
            switch (c)
            {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9': return true;
                default: return false;
            }
        }
    }
}
